package org.top10.parser;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

import java.util.regex.Pattern;

import org.cyberneko.html.parsers.DOMParser;
import org.top10.collector.Collector;
import org.top10.dealor.ConfReader;
import org.top10.dealor.FileDealor;
import org.top10.entity.CollectEntity;
import org.top10.entity.ConfBean;
import org.top10.entity.Video;
import org.top10.executor.ParseSource;
import org.top10.util.TOP10Global;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.xml.sax.InputSource;

public class XunleiTask extends Task {

	private static final int webSiteID = TOP10Global.WEB_SITE_ID_XUNLEI;// 对应网站ID
	private static final String ENCoding = "utf-8";// 对应网站ID
	public XunleiTask() {
		super(webSiteID,ENCoding);
	}

	public void loadParser() {
		PARSER.add(new XunleiVideoParser());
		PARSER.add(new XunleiMoiveParser());
		PARSER.add(new XunleiAbroadParser());
		PARSER.add(new XunleiChineseParser());
		PARSER.add(new XunleiAbroadParser());
		PARSER.add(new XunleiCartoonParser());
		PARSER.add(new XunleiArtsParser());
		PARSER.add(new XunleiHotCommentParser());
		PARSER.add(new XunleiCommentTopParser());
		PARSER.add(new XunleiGoUpFastestParser());
	}

	public static void main(String args[]) throws Exception {
		DOMParser parser = new DOMParser();
		/*
		 * 注释中为测试代码，读取src文件
		InputStream is = FileDealor.class.getClassLoader().getResourceAsStream(
				"src");
		InputStreamReader ff;
		ff = new InputStreamReader(is, "UTF-8");
		BufferedReader in = new BufferedReader(ff);
		parser.parse(new InputSource(in));
		Document doc = parser.getDocument();
		//new XunleiAbroadParser("", "").parse(doc);
		 */
		//ParseSource source = Collector.createCache("",ENCoding);
		new XunleiGoUpFastestParser().dealText(ENCoding);
	}
}

abstract class XunleiTOPParser extends TOPParserImpl {

	protected final static String DESC_PRE = "http://data.movie.xunlei.com/movie/";

	public XunleiTOPParser(String c, int t, String u) {
		super(c, t, u);
	}

	public abstract List parse(ParseSource doc) throws Exception;

	public abstract Video build(Node node, int rank) throws Exception;

	// XUNlei所有解析器的公有方法放在这里..
	protected Video buildVideo(Node li, int rank) {
		Video v = new Video();
		List<Node> as = new ArrayList<Node>();
		v.setRank(rank);
		getAllTagsByParent(li, "a", as);
		Node a1 = as.get(0);
		Node a2 = as.get(1);
		String url = getAttrValueByTag(a1, "href");
		v.setPlayUrl(url);
		if (rank == 1) {
			v.setName(a2.getTextContent());
			v.setDescUrl(getDescURL(DESC_PRE, v.getPlayUrl()));
		} else {
			v.setDescUrl(getAttrValueByTag(a2, "href"));
			v.setName(a1.getTextContent());
		}
		getAllTagsByParent(li, "strong", as);
		v.setPoint(as.get(as.size() - 1).getTextContent());
		return v;
	}

	public Video buildVideo2(Node li, int rank) throws Exception {
		Video v = new Video();
		List<Node> as = new ArrayList<Node>();
		v.setRank(rank);
		getAllTagsByParent(li, "a", as);
		Node a1 = as.get(0);
		Node a2 = as.get(1);
		String url = getAttrValueByTag(a1, "href");
		v.setPlayUrl(url);
		v.setDescUrl(getAttrValueByTag(a2, "href"));
		v.setName(a1.getTextContent());
		getAllTagsByParent(li, "strong", as);
		v.setPoint(as.get(as.size() - 1).getTextContent());
		return v;
	}

	private String getDescURL(String base, String playUrl) {
		if (playUrl == null)
			return null;
		String paths[] = playUrl.split("/");
		int pos = paths[paths.length - 1].indexOf(".");
		if (pos == -1)
			return base + paths[paths.length - 1];
		return base + paths[paths.length - 1].substring(0, pos);
	}


	protected List xunleiParse(ParseSource source, String sign) throws Exception {
		List<Video> list = new ArrayList<Video>();
		Node body = source.getDocSource().getElementsByTagName("BODY").item(0);
		Pattern p = Pattern.compile(sign, Pattern.MULTILINE);
		Node node = getNodeByPattern(body, "h2", p);
		List<Node> lis = new ArrayList<Node>();
		getAllTagsByParent(node.getParentNode(), "li", lis);
		if (lis != null) {
			for (int i = 0; i < lis.size(); i++) {
				Video v = build(lis.get(i), i + 1);
				System.out.println(v.toString());
				list.add(v);
			}
		}
		return list;
	}
}

class XunleiAbroadParser extends XunleiTOPParser {
	private final static String SIGN = "海外电影\\s*TOP10";

	public XunleiAbroadParser() {
		super("海外电影排行", TOP10Global.TYPE_Television, "http://movie.xunlei.com/top_ten/");
	}

	public List parse(ParseSource doc) throws Exception {
		return xunleiParse(doc, SIGN);
		/*
		protected List xunleiParse(ParseSource source, String sign) throws Exception {
			List<Video> list = new ArrayList<Video>();
			Node body = source.getDocSource().getElementsByTagName("BODY").item(0);
			Pattern p = Pattern.compile(sign, Pattern.MULTILINE);
			Node node = getNodeByPattern(body, "h2", p);
			List<Node> lis = new ArrayList<Node>();
			getAllTagsByParent(node.getParentNode(), "li", lis);
			if (lis != null) {
				for (int i = 0; i < lis.size(); i++) {
					Video v = build(lis.get(i), i + 1);
					System.out.println(v.toString());
					list.add(v);
				}
			}
			return list;
		}
		*/
	}

	public Video build(Node node, int rank) throws Exception {
		return buildVideo2(node, rank);
	}
}

class XunleiChineseParser extends XunleiTOPParser {
	private final static String SIGN = "华语电影\\s*TOP10";

	public XunleiChineseParser() {
		super("华语电影排行", TOP10Global.TYPE_Television,"http://movie.xunlei.com/top_ten/");
	}

	public List parse(ParseSource doc) throws Exception {
		return xunleiParse(doc, SIGN);
	}

	public Video build(Node node, int rank) throws Exception {
		return buildVideo2(node, rank);
	}
}

class XunleiCartoonParser extends XunleiTOPParser {
	private final static String SIGN = "动漫\\s*TOP10";

	public XunleiCartoonParser() {
		super("动漫", TOP10Global.TYPE_Television, "http://movie.xunlei.com/top_ten/");
	}

	public List parse(ParseSource doc) throws Exception {
		return xunleiParse(doc, SIGN);
	}

	public Video build(Node node, int rank) throws Exception {
		return buildVideo(node, rank);
	}
}

class XunleiArtsParser extends XunleiTOPParser {
	private final static String SIGN = "综艺\\s*TOP10";

	public XunleiArtsParser() {
		super("综艺", TOP10Global.TYPE_Television, "http://movie.xunlei.com/top_ten/");
	}

	public List parse(ParseSource doc) throws Exception {
		return xunleiParse(doc, SIGN);
	}

	public Video build(Node node, int rank) throws Exception {
		return buildVideo(node, rank);
	}
}

class XunleiHotCommentParser extends XunleiTOPParser {
	private final static String SIGN = "最热评影片";

	public XunleiHotCommentParser() {
		super("最热评影片", TOP10Global.TYPE_Television, "http://movie.xunlei.com/top_ten/");
	}

	public List parse(ParseSource doc) throws Exception {
		// TODO Auto-generated method stub
		return xunleiParse(doc, SIGN);
	}

	public Video build(Node node, int rank) throws Exception {
		return buildVideo2(node, rank);
	}

}

class XunleiCommentTopParser extends XunleiTOPParser {
	private final static String SIGN = "评价最高影片\\s*TOP10";

	public XunleiCommentTopParser() {
		super("评价最高影片", TOP10Global.TYPE_Television, "http://movie.xunlei.com/top_ten/");
	}

	public List parse(ParseSource doc) throws Exception {
		return xunleiParse(doc, SIGN);
	}

	public Video build(Node node, int rank) throws Exception {
		return buildVideo(node, rank);
	}

}

class XunleiGoUpFastestParser extends XunleiTOPParser {
	private final static String SIGN = "上升最快影片\\s*TOP10";

	public XunleiGoUpFastestParser() {
		super("上升最快影片", TOP10Global.TYPE_Television, "http://movie.xunlei.com/top_ten/");
	}

	public List parse(ParseSource doc) throws Exception {
		// TODO Auto-generated method stub
		return xunleiParse(doc, SIGN);
	}

	public Video build(Node node, int rank) throws Exception {
		return buildVideo(node, rank);
	}
}

class XunleiMoiveParser extends XunleiTOPParser {
	private final static String SIGN = "电影\\s*TOP10";

	public XunleiMoiveParser() {
		super("电影排行", TOP10Global.TYPE_Television, "http://movie.xunlei.com/top_ten/");
	}

	public List parse(ParseSource doc) throws Exception {
		return xunleiParse(doc, SIGN);
	}

	public Video build(Node node, int rank) throws Exception {
		return buildVideo(node, rank);
	}
}
class XunleiVideoParser extends XunleiTOPParser {
	private final static String SIGN = "上升最快视频";

	public XunleiVideoParser() {
		super("视频排行", TOP10Global.TYPE_Television, "http://video.xunlei.com/");
	}

	public List parse(ParseSource source) throws Exception {
		List<Video> list = new ArrayList<Video>();
		Node body = source.getDocSource().getElementsByTagName("BODY").item(0);
		System.out.println(body.getNodeValue());
		Pattern p = Pattern.compile(SIGN, Pattern.MULTILINE);
		Node node = getNodeByPattern(body, "h2", p);
		List<Node> lis = new ArrayList<Node>();
		getAllTagsByParent(node.getParentNode().getParentNode(), "a", lis);
		if (lis != null) {
			int i=1;
			for (Node a:lis) {
				if(a.getTextContent().trim().equals(""))continue;
				Video v = build(a, i++);
				System.out.println(v.toString());
				if(i>10)break;
				list.add(v);
				
			}
		}
		return list;
	}

	public Video build(Node node, int rank) throws Exception {
		Video v = new Video();
		v.setRank(rank);
		String url = getAttrValueByTag(node, "href");
		v.setPlayUrl(url);
		v.setName(node.getTextContent());
		return v;
	}
}